<template>
  <div class="login">
    <div class="back-content">
      <div class="back-bg"></div>
    </div>
    <h3 class="title">欣挪瑞矢量海图后台管理平台</h3>
    <div class="loginForm">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <p class="subTitle">账号密码登录</p>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <!-- <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'"
              >立即注册</router-link
            >
          </div> -->
        </el-form-item>
      </el-form>
      <el-divider direction="vertical" v-if="false"></el-divider>
    </div>
  </div>
</template>

<script>
import { setStore } from "@/utils/store";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    handlePwd() {
      const form = {
        ...this.loginForm,
        ...{ grant_type: "password", scope: "server" },
      };
      this.loading = true;
      this.$store
        .dispatch("pwdLogin", form)
        .then(() => {
          setStore({
            type: 1,
            name: "userName",
            content: this.loginForm.username,
          });
          this.$store
            .dispatch("GetInfo")
            .then(() => {
              const redirectPath = this.$route.query.redirect || "/sys/user";
              this.$router.push({
                path: redirectPath,
              });
            })
            .catch((err) => {
              this.$store.dispatch("LogOut").then(() => {
                this.$message.error(err);
              });
            });
        })
        .catch(() => {
          this.loading = false;
        });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.handlePwd();
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  padding-top: 110px;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
}
.title {
  font-size: 30px;
  color: #fff;
  font-weight: 700;
  position: relative;
}
.loginForm {
  margin-top: 80px;
  display: flex;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0px 0px 15px #000;
  position: relative;
  .subTitle {
    color: #00a0ff;
    font-size: 16px;
    line-height: 1.5;
  }
  .el-divider--vertical {
    height: 290px;
    width: 2px;
    background: rgba(255, 255, 255, 0.45);
  }
  .zldCode {
    padding: 30px 25px 5px 35px;
    .subTitle {
      text-align: center;
      color: rgba(255, 255, 255, 0.692);
      font-size: 12px;
      line-height: 1.5;
    }
    img {
      width: 195px;
    }
  }
}
.back-content {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-image: url("/login.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .back-bg {
    height: 100%;
    width: 100%;
    background-image: linear-gradient(180deg, var(--tw-gradient-stops));
    --tw-gradient-from: #4f46e5;
    --tw-gradient-stops: var(--tw-gradient-from),
      var(--tw-gradient-to, rgba(79, 70, 229, 0));
    --tw-gradient-to: #3b82f6;
    opacity: 0.75;
  }
}
.login-form {
  width: 420px;
  padding: 0 35px 5px 25px;
  .subTitle {
    margin-bottom: 25px;
  }
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  position: fixed;
  bottom: 24px;
  width: 100%;
  text-align: center;
  font-family: Arial;
  letter-spacing: 1px;
  color: hsla(0, 0%, 100%, 0.45);
  font-size: 14px;
}
.login-code-img {
  height: 38px;
}
</style>
